<template>
    <div class="pagination">
        <button :class="{disabled:pageNo===1}" @click="pageNo===1?undefined:sendPageNo(pageNo-1)">
            <svg t="1655691159030" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
                p-id="1351" width="11" height="11">
                <path
                    d="M778.671 926.323a56.811 56.811 0 1 1-80.33 80.331L243.85 552.165a56.811 56.811 0 0 1 0-80.33l454.49-454.49a56.811 56.811 0 1 1 80.33 80.331L364.348 512 778.67 926.323z"
                    p-id="1352" fill="#606266">
                </path>
            </svg>
        </button>
        <button v-if="startNumAndEndNum.start > 1" @click="sendPageNo(1)" :class="{active:pageNo==1}">1</button>
        <button v-if="startNumAndEndNum.start > 2">···</button>

        <button v-for="(page,index) in startNumAndEndNum.end" :key="index" v-show="page >= startNumAndEndNum.start"
            @click="sendPageNo(page)" :class="{active:pageNo==page}">{{page}}</button>

        <button v-if="startNumAndEndNum.end < totalPage-1">···</button>
        <button v-if="startNumAndEndNum.end < totalPage" @click="sendPageNo(totalPage)"
            :class="{active:pageNo==totalPage}">{{totalPage}}</button>
        <button :class="{disabled:pageNo == totalPage}" @click="pageNo == totalPage?undefined:sendPageNo(pageNo+1)">
            <svg t="1655690906479" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
                p-id="3789" width="11" height="11">
                <path
                    d="M236.552013 926.853955a55.805997 55.805997 0 0 0 0 80.454996 59.682997 59.682997 0 0 0 82.794996 0l468.099978-455.081978a55.805997 55.805997 0 0 0 0-80.453996L319.348009 16.689999a59.682997 59.682997 0 0 0-82.794996 0 55.805997 55.805997 0 0 0 0 80.454996L663.401993 511.999975 236.624013 926.853955z"
                    p-id="3790" fill="#606266">
                </path>
            </svg>
        </button>

        <button style="margin-left:30px" class="total">共 {{total}} 条</button>
    </div>
</template>

<script>
    import { computed, getCurrentInstance } from 'vue'
    export default {
        name: "Pagination",
        props: ["pageNo", "pageSize", "total", "continues"], //接受父组件传过来的参数
        setup(props) {
            const mitt = getCurrentInstance() //调用全局事件总线的api
            const bus = mitt.appContext.config.globalProperties.mittBus
            //计算属性
            let totalPage = computed(() => Math.ceil(props.total / props.pageSize))  //总页数（总数据/每页多少条数据 且向上取整）
            let startNumAndEndNum = computed(() => {
                //定义两个变量来储存起始页面数值和结束页面数值
                let start = 0
                let end = 0
                //当页面的总数小于连续页面数时
                if (totalPage.value < props.continues) {
                    start = 1
                    end = totalPage.value
                } else {//当页面总数大于连续页面数时
                    //起始页面数值 = 当前页面值 - 连续页面值/2 且取整
                    start = props.pageNo - parseInt(props.continues / 2)
                    //结束页面数值 = 当前页面值 - 连续页面值/2 且取整
                    end = props.pageNo + parseInt(props.continues / 2)
                    //如果起始页面数 小于 1，
                    if (start < 1) {
                        start = 1
                        end = props.continues
                    }
                    //如果结束页面数 大于 总页面数
                    if (end > totalPage.value) {
                        start = totalPage.value - props.continues + 1
                        end = totalPage.value
                    }
                }
                return { start, end }
            })
            //通过全局事件总线向父组件传递pageNo
            function sendPageNo(pageNo) {
                if (pageNo < 1) {
                    pageNo = 1
                } if (pageNo > totalPage.value) {
                    pageNo = totalPage.value
                }

                bus.emit('getPageNo', pageNo)
            }
            return {
                totalPage,
                startNumAndEndNum,
                sendPageNo
            }
        }
    }
</script>

<style lang="css" scoped>
    .pagination {
        user-select: none;
        text-align: center;
        /* margin: 40px 0 50px 0; */
    }

    button {
        margin: 0 5px;
        background-color: #fff;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;
    }

    button:hover {
        background-color: #f1f1f1;
        color: #4F68AC;
    }

    .disabled {
        cursor: not-allowed;
    }

    .disabled svg path {
        fill: #c0c4cc;
    }

    .disabled:hover {
        background-color: #fff;
    }

    .active {
        cursor: pointer;
        background-color: #4F68AC;
        color: #fff;
    }

    .active:hover {
        background-color: #4F68AC;
        color: #fff;
    }

    .total:hover {
        background-color: #fff;
        color: #606266;
        cursor: auto;
    }
</style>